<?php echo "<?php\n"; ?>
	
	/**
	 * <?= $this->controller_name ?> Controller File
	 * 
	 * @author theFramework
	 * @version 1.0
	 * @package myApp
	 */
	
	/**
	 * <?= $this->controller_name ?> class
	 *
	 * @package myApp
	 **/
	
	Class <?= $this->controller_name ?> Extends BaseController
	{
		
		function <?= $this->controller_name ?>()
		{
			$this->layout = "default";
		}
		
		public function index()
		{
			$this->render('listing');
		}
		
		public function listing()
		{
			// get the page number
			$this->page = 1;
			if(isset($_GET['page'])){ $this->page = sanitize($_GET['page']); }

			// find all the $model_tn_plural for this page
			$this-><?= $this->model_tn ?> = new <?= $this->model_name ?>();
			$this-><?= $this->model_tn_plural ?> = $this-><?= $this->model_tn ?>->find('all', 'none', 10, ($this->page - 1) * 10);
		}
		
		public function show()
		{
			// get the id for the <?= $this->model_name ?>
			$id = 0;
			if(isset($_GET['id'])){ $id = sanitize($_GET['id']); }
			
			// load the <?= $this->model_name ?>
			$this-><?= $this->model_tn ?> = new <?= $this->model_name ?>();
			if(! $this-><?= $this->model_tn ?>->load($id) )
			{
				// loading failed
				flash('error', "Sorry, the item you where looking for could not be found");
			}
		}
		
		public function add()
		{
			$this-><?= $this->model_tn ?> = new <?= $this->model_name ?>();
		}
		
		public function create()
		{
			$this-><?= $this->model_tn ?> = new <?= $this->model_name ?>();
			
			<?php
				$model = new {$this->model_name}();
				foreach($model->getTableColumns() as $row)
				{
					if($row['Key'] != "PRI")
					{
						$col_name = ucfirst($row['Field']);
			?>
			if(isset($_POST["<?= $row['Field'] ?>"]))
			{
				$this-><?= $this->model_tn ?>-><? $col_name ?>(sanitize($_POST["<?= $row['Field'] ?>"]));
			}
			
			<?php
					}
				}
			?>
			
			if( $this-><?= $this->model_tn ?>->save() )
			{
				flash("notice", "<?= $this->model_name ?> was create successfully");
				$this->redirect('$controller_tn', 'show', "$this->$model_tn");
			}else{
				flash("error", "<?= $this->model_name ?> was not created please try again");
				$this->redirect("<?= $this->controller_tn ?>", "add");
			}
			
		}
		
		function edit()
		{
			// get the id
			$id = 0;
			if(isset($_GET['id'])){ $id = sanitize($_GET['id']); }
			
			// load the <?= $this->model_name ?>
			$this-><?= $this->model_tn ?> = new <?= $this->model_name ?>();
			if(! $this-><?= $this->model_tn ?>->load($id))
			{
				// couldn't load the item so redirect to listing
				flash("error", "Sorry, that <?= $this->model_name ?> does not exist");
				$this->redirect("<? $this->controller_tn ?>", "listing");
			}
		}
		
		function update()
		{
			// get the id
			$id = 0;
			if(isset($_GET['id'])){ $id = sanitize($_GET['id']); }
			
			// load the <?= $this->model_name ?>
			$<?= $this->model_tn ?> = new <?= $this->model_name ?>();
			if( $<?= $this->model_tn ?>->load($id) )
			{
				<?php
					$model = new $this->model_name();
					foreach($model->getTableColumns() as $row)
					{
						if($row['Key'] != "PRI")
						{
							$col_name = ucfirst($row['Field']);
				?>
				if(isset($_POST["<?= $row['Field'] ?>"]))
				{
					$<?= $this->model_tn ?>-><? $col_name ?>(sanitize($_POST["<?= $row['Field'] ?>"]));
				}
				<?php
						}
					}
				?>
				
				$<?= $this->model_tn ?>->save();
			}
			
			$this->redirect("<?= $this->controller_tn ?>", "show", $<?= $this->model_tn ?>-><?= $model->getPrimaryKey() ?>);
		}
		
		function delete()
		{
			// get the id
			$id = 0;
			if(isset($_GET['id'])){ $id = sanitize($_GET['id']); }
			
			// load the model
			$<?= $this->model_tn ?> = new <?= $this->model_name ?>();
			if($<?= $this->model_tn ?>->load($id))
			{
				// delete the model
				$<?= $this->model_tn ?>->destroy($id);
				flash("notice", "Successfully deleted <?= $this->model_name ?>");
			}else{
				flash("error", "Sorry, that <?= $this->model_name ?> does not exist");
			}
			
			$this->redirect("<? $this->controller_tn ?>", "listing");
		}
		
	}
	
